﻿
/*
* iwss-lib JavaScript库
* @namespace iwss.validate
* @name validate
*/
(function (my) {
    my.validate = {
        check: function () {
            var allValidateControls = $("[validate=true]");
            if (allValidateControls.length == 0) { return true; }
            var msg = '';

            var result = true;
            allValidateControls.each(function () {
                var self = $(this);
                msg = self.attr('msg');
                var type = self.attr("type");
                var res = false;

                switch (type) {
                    case "checkbox":
                        res = iwss.validate.checkbox(self);
                        break;
                    case "time":
                        res = iwss.validate.time(self);
                        break;
                    case "input":
                        res = iwss.validate.input(self);
                        break;
                }

                if (!res) {
                    result = res;
                    return false;
                }
            });
            if (!result)
                alert(msg);

            return result;
        },
        input: function (obj) {
            if (obj.is("input"))
                return obj.val().trim() != '';
            else {
                var flag = true;
                obj.find("input").each(function () {
                    var self = $(this);
                    if (self.val().trim() == '') {
                        flag = false;
                        return false;
                    }
                })
                return flag;
            }
        },
        checkbox: function (obj) {
            if (obj.is("input")) {
                return obj.attr("checked") == true;
            }
            return obj.find("input[type=checkbox]:checked").length > 0 || obj.find("input[type=radio]:checked").length > 0;
        },
        time: function (obj) {
            var pickerID = "SP" + obj.attr("pickerid");
            var myd = $("#" + g_strDateTimeControlIDs[pickerID]);
            var myh = $('#' + myd.attr('id') + 'Hours');
            var mym = $('#' + myd.attr('id') + 'Minutes');

            var d = new Date(String.format("{0} {1}{2}", myd.val(), myh.val(), mym.val()));
            return d > new Date();
        }
    }
    return my;
} (iwss || {}));